package com.agileai.bpm.controller.define;

import java.util.ArrayList;
import java.util.List;

import com.agileai.bpm.bizmoduler.define.WfProcessManage;
import com.agileai.bpm.common.ProcessDefineUtil;
import com.agileai.bpm.controller.emulation.ProcessHelper;
import com.agileai.bpm.model.BizAttributeType;
import com.agileai.bpm.model.DataFieldsDocument.DataFields;
import com.agileai.bpm.model.FlowLauncherType;
import com.agileai.bpm.model.InterceptorModelDocument.InterceptorModel;
import com.agileai.bpm.model.ProcessDefineDocument;
import com.agileai.bpm.model.ProcessDefineDocument.ProcessDefine;
import com.agileai.bpm.model.VariableType;
import com.agileai.bpm.model.WatchInvokerType;
import com.agileai.bpm.webservice.BPMService;
import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.StringUtil;

public class WfVariableCodeSelectHandler extends WfProcessInfoHandler{
	public static final String TEMP_INTERCEPTORID  = "temp_interceptorId";
	
	public WfVariableCodeSelectHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param){
		this.setAttributes(param);
		this.setAttribute(param, "fromHandlerId");
		
		BPMService bpmService = ProcessHelper.instance().getBPMService();
		String processCode = param.get("processCode");
		String processId = bpmService.getCurrentProcessId(processCode);
		DataParam queryParam = new DataParam("WFP_ID",processId);
		WfProcessManage processManage = this.lookupService(WfProcessManage.class);
		DataRow row = processManage.getRecord(queryParam);
		String processDefineXML = row.getString("WFP_DEFINE");
		try {
			ProcessDefineDocument processDefineDocument = ProcessDefineDocument.Factory.parse(processDefineXML);
			DataFields dataFields = processDefineDocument.getProcessDefine().getDataFields();
			List<DataRow> processVars = null;
			if (dataFields != null){
				processVars = buildProcessVars(dataFields);
			}
			this.setRsList(processVars);
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new LocalRenderer(getPage());
	}
	
	private List<DataRow> buildProcessVars(DataFields dataFields){
		List<DataRow> result = new ArrayList<DataRow>();
		VariableType[] variableTypes = dataFields.getDataFieldArray();
		if (variableTypes != null){
			for (int i=0; i< variableTypes.length;i++){
				VariableType variableType = variableTypes[i];
				DataRow row = new DataRow();
				if (StringUtil.isNullOrEmpty(variableType.getDescription())){
					row.put("VAR_DESC",variableType.getCode());	
				}else{
					row.put("VAR_DESC",variableType.getDescription());					
				}
				row.put("VAR_CODE",variableType.getCode());
//				row.put("VAR_TYPE",variableType.getDataType());
//				row.put("VAR_INIT_VALUE",variableType.getInitialValue());
//				row.put("IS_VAR",variableType.getIsVariable());
				result.add(row);
			}
		}
		return result;
	}
	
	public ViewRenderer doSelectAction(DataParam param){
		String activityId = param.get("WFA_ID");
		String interceptorId = param.get("interceptorId");
		String positionType = param.get("positionType");
		String fromHandlerId = param.get("fromHandlerId");
		String backHandlerURL = null;
		if (!StringUtil.isNullOrEmpty(interceptorId)){
			ProcessDefine processDefine = getProcessDefine();
			InterceptorModel interceptorModel = ProcessDefineUtil.getInterceptorModel(processDefine, positionType, activityId, interceptorId);
			if (interceptorModel.isSetFlowLauncher()){
				FlowLauncherType flowLauncherType = interceptorModel.getFlowLauncher();
				BizAttributeType bizAttributeType = flowLauncherType.addNewBizAttribute();
				String variableCode = param.get("variableCode");
				bizAttributeType.setCode(variableCode);
			}
			else if (interceptorModel.isSetWatchInvoker()){
				WatchInvokerType watchInvokerType = interceptorModel.getWatchInvoker();
				BizAttributeType bizAttributeType = watchInvokerType.addNewBizAttribute();
				String variableCode = param.get("variableCode");
				bizAttributeType.setCode(variableCode);
			}
			backHandlerURL = getHandlerURL(fromHandlerId);
		}else{
			ProcessDefine processDefine = getProcessDefine();
			InterceptorModel interceptorModel = ProcessDefineUtil.createInterceptorModel(processDefine, positionType, activityId);
			interceptorId = KeyGenerator.instance().genKey();
			interceptorModel.setId(interceptorId);
			
			if ("WfProcessInvokerEdit".equals(fromHandlerId)){
				WatchInvokerType watchInvokerType = interceptorModel.addNewWatchInvoker();
				BizAttributeType bizAttributeType = watchInvokerType.addNewBizAttribute();
				String variableCode = param.get("variableCode");
				bizAttributeType.setCode(variableCode);
			}else{
				FlowLauncherType flowLauncherType = interceptorModel.addNewFlowLauncher();
				BizAttributeType bizAttributeType = flowLauncherType.addNewBizAttribute();
				String variableCode = param.get("variableCode");
				bizAttributeType.setCode(variableCode);
			}
			this.getSessionAttributes().put(TEMP_INTERCEPTORID, interceptorId);
			backHandlerURL = getHandlerURL(fromHandlerId);
		}
		return new DispatchRenderer(backHandlerURL);
	}
	public ViewRenderer doBackAction(DataParam param){
		String fromHandlerId = param.get("fromHandlerId");
		return new DispatchRenderer(getHandlerURL(fromHandlerId));
	}

}
